package io.coderyeah.basic.controller;

import io.coderyeah.basic.Result;
import io.coderyeah.basic.dto.PhoneCodeDTO;
import io.coderyeah.basic.exception.BusinessException;
import io.coderyeah.basic.service.VerifyCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
@Api(value = "验证码接口", tags = "验证码")
public class VerifyCodeController {
    @Autowired
    private VerifyCodeService verifyCodeService;

    @GetMapping("/image/{key}")
    @ApiOperation("获取图形验证码")
    public Result getImgCode(@PathVariable("key") String key) {
        try {
            return verifyCodeService.getImgCode(key);
        } catch (BusinessException e) {
            e.printStackTrace();
            return Result.fail("获取验证码失败！" + e.getMessage());
        } catch (Exception e) {
            return Result.fail("系统异常,请稍后重试!");
        }
    }

    @PostMapping("/smsCode")
    @ApiOperation("获取短信验证码")
    public Result getPhoneCode(@RequestBody PhoneCodeDTO phoneCodeDTO) {
        try {
            return verifyCodeService.getPhoneCode(phoneCodeDTO);
        } catch (BusinessException e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        } catch (Exception e) {
            return Result.fail("系统异常,请稍后重试!");
        }
    }

    /**
     * 获取手机验证码
     *
     * @return
     */
    @PostMapping("/binderSmsCode")
    public Result binderSmsCode(@RequestBody PhoneCodeDTO codeDTO) {
        try {
            verifyCodeService.binderSmsCode(codeDTO);
            return Result.success(null);
        } catch (BusinessException e) {
            return Result.fail("发送失败，" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统繁忙");
        }
    }
}
